#!/bin/bash
#安装常用的软件包
yum install -y nmap unzip wget lsof xz net-tools gcc make gcc-c++

#下载redis-5.0.5的版本
redis_setup=5.0.5
redis_path=/home/redis
port=6379
wget http://download.redis.io/releases/redis-${redis_setup}.tar.gz

#解压redis安装包
tar zxvf redis-${redis_setup}.tar.gz

#创建redis安装日志、启动、配置文件等目录
cd redis-${redis_setup}
mkdir  -p ${redis_path}
mkdir  -p ${redis_path}/bin
mkdir  -p ${redis_path}/log
mkdir  -p ${redis_path}/conf
cp redis.conf ${redis_path}/conf

#编译并安装
make MALLOC=jemalloc && make PREFIX=${redis_path} install

#修改配置文件，可以后台启动
sed -i 's/daemonize no/daemonize yes/g' ${redis_path}/conf/redis.conf
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g'  ${redis_path}/conf/redis.conf
sed -i 's/port 6379/port '${port}'/'  ${redis_path}/conf/redis.conf
sed -i "s#pidfile \/var\/run\/redis_6379.pid#pidfile ${redis_path}\/redis_${port}.pid#g" ${redis_path}/conf/redis.conf
sed -i "s#logfile \"\"#logfile ${redis_path}\/log\/redis.log#g"  ${redis_path}/conf/redis.conf
#优化参数
echo vm.overcommit_memory=1 >> /etc/sysctl.conf
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "net.core.somaxconn=1024">> /etc/sysctl.conf
sysctl -p

#加入到开机的环境变量里面去
echo "export PATH=${redis_path}/bin:$PATH">>/etc/profile
source /etc/profile

#启动redis
redis-server ${redis_path}/conf/redis.conf

#检测redis是否启动
status=`netstat -nltp|grep ${port}|grep -v "grep"`
if [ -z "${status}" ];then
  echo "please check the config"
else
  echo "redis successful install"
fi

